﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using BDElectron.Platform;

namespace BDElectron.Server
{
    class Program
    {
        static void Main(string[] args)
        {
            Logger.LogInfo(string.Empty);
            Logger.LogInfo("----------------------------------------->>>");
            Logger.LogInfo("HRPM Server application is starting");

            try
            {
                // Initialize
                string remotingInitMessage = "Initializing remoting";
                Logger.LogInfo(remotingInitMessage);
                String appName = System.Reflection.Assembly.GetExecutingAssembly().Location;
                RemotingConfiguration.Configure(appName + ".config", false);

                string startupMessage = "HRPM Server application is started";
                Logger.LogInfo(startupMessage);

                string waitEnterMessage = "Press ENTER to stop HRPM Server...";
                WriteColoredConsoleMessage(waitEnterMessage, ConsoleColor.Yellow);

                // Wait for input
                bool supressInput = true;
                ConsoleKeyInfo consoleKey;
                do
                {
                    consoleKey = Console.ReadKey(supressInput);
                }
                while (consoleKey.Key != ConsoleKey.Enter);


                // Cleanup and say "bye"
                string shutdownMessage = "HRPM Server application is stopping...";
                WriteColoredConsoleMessage(shutdownMessage, ConsoleColor.Yellow);
                Logger.LogInfo(shutdownMessage);

                string goodbyeMessage = "HRPM Server says: GOODBYE!";
                WriteColoredConsoleMessage(goodbyeMessage, ConsoleColor.Yellow);

                System.Threading.Thread.Sleep(5000);
                Logger.LogInfo("<<<-------------------------------");

            }
            catch (Exception e)
            {
                WriteColoredConsoleMessage("=== ATTENTION! SERVER MAY BE UNUSABLE  ====", ConsoleColor.Red);
                WriteColoredConsoleMessage("=== EXCEPTION: " + e.Message, ConsoleColor.Red);   
            }
        }

        static void WriteColoredConsoleMessage(string message, ConsoleColor consoleColor)
        {
            ConsoleColor oldColor = Console.ForegroundColor;
            Console.ForegroundColor = consoleColor;
            Console.WriteLine(message);
            Console.ForegroundColor = oldColor;
        }


    }
}
